package kr.hkit.powerjava.project_cafe;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class DBProperties {
	//load, save, prn
	
	public static Properties loadProperties(String PropertiesFile){
		
		BufferedInputStream bis = null; 
		Properties prop = null;
		
		try {
			bis = new BufferedInputStream(new FileInputStream(PropertiesFile));
			prop = new Properties();
			prop.loadFromXML(bis);
		} catch (FileNotFoundException e) {
			System.out.println("파일 찾을 수 없음!!");
		} catch (InvalidPropertiesFormatException e) {
			System.out.println("유효하지 않은 파일!!");
		} catch (IOException e) {
			e.printStackTrace();
		} finally { //스트림이라서 닫아줘야함
			if (bis != null) {
				try {bis.close(); bis=null;} catch (IOException e) {}
			}
		}
		return prop;
	}
	
	// savePropertis("test.prof", "user", "pass", "db"); 
	//value란 이름의 배열로 담겨짐. 가변길이인수
	public static void saveProroperties(String PropertiesFile, String... value){
		
		BufferedOutputStream bos = null;
		Properties prop = null;

		try {
			bos =new BufferedOutputStream(new FileOutputStream(PropertiesFile));
			prop = new Properties();
			prop.setProperty("DRIVER", value[0]); 
			prop.setProperty("URL", value[1]);
			prop.setProperty("USER", value[2]);
			prop.setProperty("PASSWORD", value[3]);
			prop.storeToXML(bos, "DB연결용 Property File");
			
		} catch (FileNotFoundException e) {
			System.err.print("파일이 존재하지 않음");
		} catch (IOException e) {
		} finally {
			if (bos != null) {
				try {bos.close(); bos=null;} catch (IOException e) {}
			}
		}
	}
	
	public static void prnProperties (Properties prop) {
		Set<Entry<Object, Object>> setmap = prop.entrySet();	
		
		for (Entry<Object, Object> proty : setmap) {
			System.out.printf("%s : s%n", proty.getKey(), proty.getValue());
		}
	}
}





